package cn.edu.scau.cmi.maikejian.composite.consistent;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.*;


public class Controller {
	
	
	Map<Team, TreeItem<String>> teamItem = new HashMap<Team, TreeItem<String>>();
	Map<Personal, TreeItem<String>> personItem = new HashMap<Personal, TreeItem<String>>();
	Map<String, Personal> bigperson = new HashMap<String, Personal>(); //在总目录下的个人
	Map<String, Team> bigteam = new HashMap<String, Team>();
	Map<String, Personal> person = new HashMap<String, Personal>();
	Map<String, Team> team = new HashMap<String, Team>();
	TreeItem<String> rootItem = new TreeItem<> ("总目录");
	String FFF;
    @FXML
    private Button add;
    @FXML
    private TextField tip;
    @FXML
    private Button addleaf;
    @FXML
    private TextField parent;

    @FXML
    private TextField PERSON;

    @FXML
    private TextField TEAM;
    @FXML
    private TreeView<String> treeview;

    @FXML
    private Button deleteteam;
    
    @FXML
    private Button deleteperson;

    public void initialize() {
    	
        rootItem.setExpanded(true);
        // 每个Item下又可以添加新的Item
      treeview.setRoot(rootItem);

    }
    @FXML
    void addzujian(ActionEvent event) {
    	String Pteam = parent.getText();
    	String team2 = TEAM.getText();
    	
    	
    	Team t = new Team(team2);
  if(!person.containsKey(team2)&&!team.containsKey(team2)&&!bigperson.containsKey(team2)&&!bigteam.containsKey(team2))  
  {
    	if(bigteam.containsKey(Pteam)||team.containsKey(Pteam)||Pteam.equals(""))
    	{
    		Object object;
    		Team Pt;
    		if(!Pteam.equals("")) {
    		 if(team.containsKey(Pteam))
    		 {
    	   object = team.get(Pteam);
    	   Pt = (Team)object;   
    		 }else {
    			 object = bigteam.get(Pteam);
    	    	   Pt = (Team)object;  
			}
          Pt.addChild(t);
    	  TreeItem<String> treeItem = new TreeItem<String>("团队:"+t.getName());
    	  team.put(team2,t);
    	  teamItem.put(t, treeItem);
    	  object = teamItem.get(Pt);
    	  TreeItem<String> ftreeItem = (TreeItem<String>)object ;
    	  ftreeItem.getChildren().add(treeItem);
                              }
    	                     else
                        	  {
                         		 
                         			TreeItem<String> treeItem = new TreeItem<String>("团队:"+t.getName());
                         	    	bigteam.put(team2,t);
                         	    	teamItem.put(t, treeItem);  
                         	    	rootItem.getChildren().add(treeItem);
    	                      } 
    	
    	}
    	else 
    		tip.setText("你的父团队名是同级下的个人名或没有此父团队");
  } 	
  else    
	  tip.setText("重名现象\n");
    }

    @FXML
    void addleaf(ActionEvent event) {
    	String Pteam = parent.getText();
    	String person1 = PERSON.getText();
    	
    	
    	Personal p = new Personal(person1);
    	
    	 if(!person.containsKey(person1)&&!team.containsKey(person1)&&!bigperson.containsKey(person1)&&!bigteam.containsKey(person1))  
    	  {
    	    	if(team.containsKey(Pteam)||bigteam.containsKey(Pteam)||Pteam.equals(""))
    	    	{
    	    	 if(!Pteam.equals("")) {
    	    		 Team Pt;
    	    		 Object object;
    	    		if(team.containsKey(Pteam)) 
    	    		{
    	    	 object = team.get(Pteam);
    	    	     Pt = (Team)object;  
    	    		}
    	    		else
    	    		{
    	    			  object = bigteam.get(Pteam);
    	    	    	     Pt = (Team)object;  
    	    		}	
    	          Pt.addChild(p);
    	    	  TreeItem<String> treeItem = new TreeItem<String>("个人:"+p.getName());
    	    	  person.put(person1,p);
    	    	  personItem.put(p, treeItem);
    	    	  object = teamItem.get(Pt);
    	    	  TreeItem<String> ftreeItem = (TreeItem<String>)object ;
    	    	  ftreeItem.getChildren().add(treeItem);
    	                              }
    	    	                     else
    	                        	  {
    	                         		 
    	    	                    	 TreeItem<String> treeItem = new TreeItem<String>("个人:"+p.getName());
    	    	                  
    	    	           	    	       bigperson.put(person1,p);
    	    	           	    	       personItem.put(p, treeItem);
    	    	           	        	   rootItem.getChildren().add(treeItem);
    	    	                      } 
    	    	
    	    	}
    	    	else 
    	    		tip.setText("你的父团队名是同级下的个人名或没有此父团队");
    	  } 	
    	  else    
    		  tip.setText("重名现象");
    	
    }
    
    @FXML
    void deleteperson(ActionEvent event) {
    	   String person1 = PERSON.getText();
    	   String Pteam = parent.getText();
    	   if(person1.equals(""))
    		   tip.setText("人名为空");
    	   else if(person.containsKey(person1))
    	   {
    		  if(team.containsKey(Pteam)) 
    		  {
    		  Object object = person.get(person1);
    		  Personal personal = (Personal)object;
    		  object = personItem.get(personal);
    		  TreeItem<String> treeItem = (TreeItem<String>)object;
    		  
    		    object = team.get(Pteam);
    		  Team team =  (Team)object;
    		  object = teamItem.get(team);
    		  TreeItem<String> ftreeItem = (TreeItem<String>)object;
    		  
    		  
    		 if( ftreeItem.getChildren().remove(treeItem))
    		 {
    			 team.deleteChild(personal);
    			 person.remove(person1);
    			 personItem.remove(personal);
    			 tip.setText("删除成功"); 
    		 }
    		  }
    		  else if(bigteam.containsKey(Pteam))
    		  {
    			  Object object = person.get(person1);
        		  Personal personal = (Personal)object;
        		
        		  object = personItem.get(personal);
        		  TreeItem<String> treeItem = (TreeItem<String>)object;
        		  
        		    object = bigteam.get(Pteam);
        		  Team team =  (Team)object;
        		  object = teamItem.get(team);
        		  TreeItem<String> ftreeItem = (TreeItem<String>)object;
        		  
        		 
        		 if( ftreeItem.getChildren().remove(treeItem))
        		 {  
        			 team.deleteChild(personal);
        			 person.remove(person1);
        			 personItem.remove(personal);
        			 tip.setText("删除成功"); 
    		    }
    		  }
    		  else {
    			  tip.setText("没有找到父类"); 
			}
    	   }
    	   else if(bigperson.containsKey(person1))
    	   {
    		
    		   Object object = bigperson.get(person1);
     		  Personal personal = (Personal)object;
     		 
     		  object = personItem.get(personal);
     		  TreeItem<String> treeItem = (TreeItem<String>)object;
     		  
     		 if( rootItem.getChildren().remove(treeItem))
     		 {
     			tip.setText("删除成功");
     		   bigperson.remove(person1);
     		    personItem.remove(personal);
     		 }
     		 else
     			tip.setText("删除不成功"); 
    	   }
    	   else
    		   tip.setText("人名不存在"); 
    	   
    }

    @FXML
    void deleteteam(ActionEvent event) {
              String team2 = TEAM.getText();
              String Pteam = parent.getText();
       	   if(team2.equals(""))
       		   tip.setText("名为空");
       	   else if(team.containsKey(team2))
       	   {
       		  if(team.containsKey(Pteam)) 
       		  {
       		  Object object = team.get(team2);
       		  Team t  = (Team)object ;
       		  object = teamItem.get(t);
       		  TreeItem<String> treeItem = (TreeItem<String>)object;

       		    object = team.get(Pteam);
       		  Team team =  (Team)object;
       		  object = teamItem.get(team);
       		  TreeItem<String> ftreeItem = (TreeItem<String>)object;
       		             if( ftreeItem.getChildren().remove(treeItem))
       		          {
       		            	 delectChild(t, teamItem, personItem, bigperson, bigteam, person, this.team);
       		            	 t.getChildren().clear();
       		    	         team.deleteChild(t);
       		    	        
       		    	      this.team.remove(team2);
       	            		 teamItem.remove(t);
       		        	 tip.setText("删除成功"); 
       		          }
       		  }
       		  else if(bigteam.containsKey(Pteam))
       		  {
       			 Object object = team.get(team2);
          		  Team t  = (Team)object ;
          		  object = teamItem.get(t);
         		  TreeItem<String> treeItem = (TreeItem<String>)object;
           		    object = bigteam.get(Pteam);
           		  Team team =  (Team)object;
           		  object = teamItem.get(team);
           		  TreeItem<String> ftreeItem = (TreeItem<String>)object;
           		 
           		 if( ftreeItem.getChildren().remove(treeItem))
           		 {  
           			delectChild(t, teamItem, personItem, bigperson, bigteam, person, this.team);
           		 t.getChildren().clear();
           			 team.deleteChild(t);
           			 this.team.remove(team2);
           			 teamItem.remove(t);
           			 tip.setText("删除成功"); 
       		    }
           		else
         			tip.setText("删除不成功"); 
       		  }
       		  else {
       			  tip.setText("没有找到父类"); 
   			}
       	   }
       	   else if(bigteam.containsKey(team2))
       	   {
       		

     			 Object object = bigteam.get(team2);
        		  Team t  = (Team)object ;
         		
        		  object = teamItem.get(t);
       		  TreeItem<String> treeItem = (TreeItem<String>)object;
        		  
        		 if( rootItem.getChildren().remove(treeItem))
        		 {
        			 delectChild(t, teamItem, personItem, bigperson, bigteam, person, this.team);
        			 t.getChildren().clear();
        			 bigteam.remove(team2);
          			 teamItem.remove(t);
          			 tip.setText("删除成功"); 
        		 }
        		 else
        			tip.setText("删除不成功"); 
       	   }
       	   else
       		   tip.setText("名不存在"); 
    }
    void delectChild(Team t,Map<Team, TreeItem<String>> TeamItem,Map<Personal, 
    		TreeItem<String>> PersonItem,Map<String, Personal> Bigperson,Map<String, Team> Bigteam,Map<String, Personal> Person,Map<String, Team> TEam)
    {
    	TeamItem.remove(t);
    	if(TEam.containsKey(t.getName()))
    		TEam.remove(t.getName());
    	else if(Bigteam.containsKey(t.getName()))
    		Bigteam.remove(t.getName());
    	  Set<Orangization> set = t.getChildren();
    	  for (Orangization C : set) {
    	     if(C.type.equals("Personal"))
    	     {
    	    	     Personal p = (Personal)C;
    	    	     PersonItem.remove(p);
    	    	    if(Person.containsKey(p.getName()))
    	     		Person.remove(p.getName());
    	     	    else if(Bigperson.containsKey(p.getName()))
    	     		Bigperson.remove(p.getName());
    	     }
    	     else {
    	    	 Team t1 = (Team)C;
    	    	 delectChild(t1, TeamItem, PersonItem, Bigperson, Bigteam, Person, TEam);
    	     }
    	}
    }
}
